//插入
function insertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare);
  if (index === 0) {
    return;
  }
  if (index > 0) {
    arr.splice(index, 1);
  }
  arr.unshift(val);
  //先进先出队列
  if (maxLen && arr.length > maxLen) {
    arr.pop();
  }
}
//移除
function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare);
  if (index > -1) {
    arr.splice(index, 1);
  }
}
//保存歌曲
export function save(item, key, compare, maxLen) {
  let items = localStorage.getItem(key) || [];
  items.length ? (items = JSON.parse(items)) : null;
  insertArray(items, item, compare, maxLen);
  localStorage.setItem(key, JSON.stringify(items));
  return items;
}
//移除歌曲
export function remove(key, compare) {
  let items = localStorage.getItem(key) || [];
  items.length ? (items = JSON.parse(items)) : null;
  deleteFromArray(items, compare);
  localStorage.setItem(key, JSON.stringify(items));
  return items;
}
export function load(key) {
  return JSON.parse(localStorage.getItem(key)) || [];
}
export function clear(key) {
  localStorage.removeItem(key);
  return [];
}
